﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.IdentityModel.Tokens.Jwt;
using tju.cme.BookStore2024.webapi.Models.Tools;
using tju.cme.BookStore2024.webapi.Models.ViewModels.Admin;
using tju.cme.BookStore2024.webapi.Models.ViewModels.Author;
using tju.cme.BookStore2024.webapi.Services;

namespace tju.cme.BookStore2024.webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdminController(ILogger<AdminController> logger ,AdminServices services) : ControllerBase
    {
        ///// <summary>
        ///// 上传特定章节
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpPost("Upload/{id}")]
        //public async Task<IActionResult> Upload(Guid id)
        //{
        //    await services.UploadChapterAsync(id);
        //    return Ok(new ReturnMessage() { IsSuccess = true });
        //}



        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("Login")]
        public async Task<IActionResult> Login(AdminLoginModel model)
        {
            var token = await services.AdminLoginAsync(model);
            logger.LogInformation($"{model.UserName}登录成功");
            return Ok(new ReturnMessage(new JwtSecurityTokenHandler().WriteToken(token)));
        }



        /// <summary>
        /// 创建管理员账户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<IActionResult> Create(AdminCreateModel model)
        {
            await services.CreateAdminAsync(model);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


       
    }
}
